// 管理员相关接口,管理员登录
const express = require("express")
const router = express.Router()
// 引入连接池
const pool = require("../pool/pool")
// 引入封装后的响应
const Response = require("../utils/Response")

const { generateToken } = require("../core/utils");
// 登录接口
router.post("/admin_login", (req, res) => {
  let obj = req.body
  let admin = obj.d_admin
  let pwd = obj.d_pwd
  let sql = `select * from pet_admin where d_admin = ? and d_pwd = ?`
  pool.query(sql, [admin, pwd], (err, data) => {
    if (err) {
      throw err
    }
    console.log(data)
    if (res.length == 0) {
      res.send({ code: "201", msg: "登录失败，管理员信息不存在！" })
    } else {
       const token = generateToken(data[0].d_id, 2);
       res.send({
         code: 200,
         msg: "登录成功!",
         token: token,
         d_admin: data[0].d_admin,
         d_pwd: data[0].d_pwd
      })
    }
  })
})

module.exports = router
